package com.lxg.service;

import com.lxg.dao.UserDao;
import com.lxg.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author 陆小根
 * date: 2022/04/24
 * Description:
 */

@Service
public class MyUserDetailsService implements UserDetailsService {

  private UserDao userDao;

  @Autowired
  public MyUserDetailsService(UserDao userDao) {
    this.userDao = userDao;
  }

  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = this.userDao.loadUserByUsername(username);
    if (ObjectUtils.isEmpty(user)) throw new RuntimeException("用户名不存在");
    user.setRoles(userDao.getRolesByUid(user.getId()));
    return user;
  }
}
